import 'package:flutter/material.dart';
import 'package:gzmall_app/common/color_common.dart';
import '../goods/goods_list_widget.dart';
import 'package:gzmall_app/brands/brands_details_page.dart';

class CategoryChildrenListItem extends StatelessWidget {
  var category;
  String type = "ShopCategory";
  List children = [];

  CategoryChildrenListItem(category, {this.type}) {
    this.category = category;
  }

  List<Widget> getCategoryChildrenWidgets(
      BuildContext context, List categories) {
    double width = (MediaQuery.of(context).size.width - 100 - 30 - 48) / 3;
    return categories
        .map((item) => new InkWell(
              child: new Container(
                width: width,
                child: new Column(
                  children: <Widget>[
                    new Container(
                      margin: const EdgeInsets.only(bottom: 8.0),
                      width: width,
                      height: width,
                      decoration: new BoxDecoration(
                        image: new DecorationImage(
                            image: new NetworkImage(item['avatar']),
                            fit: BoxFit.fill),
                      ),
                    ),
                    new Text(
                      item["name"],
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style:
                          TextStyle(fontSize: 12.0, color: ColorCommon.grey8),
                    ),
                  ],
                ),
              ),
              onTap: () {
                switch (type) {
                  case "Brand":
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (_) {
                      return new BrandsDetailsPage(id: item["id"]);
                    }));
                    break;
                  default:
                    Navigator.of(context)
                        .push(new MaterialPageRoute(builder: (_) {
                      return new GoodsListWidget(
                        categoryId: item["id"],
                        categoryName: item["name"],
                        level: item["level"],
                      );
                    }));
                }
              },
            ))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    switch (type) {
      case "Brand":
        children = category['brands'];
        break;
      default:
        children = category['children'];
    }
    return new Container(
        margin: EdgeInsets.only(top: 24.0, left: 15.0, right: 15.0),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(bottom: 20.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Text(
                      category['name'],
                      style:
                          TextStyle(fontSize: 12.0, color: ColorCommon.black1),
                    ),
                  ),
                  // Text(
                  //   "查看全部",
                  //   style: TextStyle(fontSize: 15.0, color: ColorCommon.grey3),
                  // ),
                ],
              ),
            ),
            new Wrap(
                spacing: 24.0, // gap between adjacent chips
                runSpacing: 14.0, // gap between lines
                children: children != null && children.length > 0
                    ? getCategoryChildrenWidgets(context, children)
                    : [
                        new Center(
                            child: new Padding(
                          padding: const EdgeInsets.symmetric(vertical: 20.0),
                          child: new Text('没有符合条件的分类.'),
                        ))
                      ])
          ],
        ));
  }
}
